package com.haiwen.controller;

import com.haiwen.vo.responseData;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@RestController
public class uploadController {
    //上传文件会自动绑定到MultipartFile中
    @PostMapping(value="/upload.do")
    public responseData upload(HttpServletRequest request,
                               @RequestParam("EssName") String EssName,
                               @RequestParam("EssText") String EssText,
                               @RequestParam("file") MultipartFile file) throws Exception {
        //如果文件不为空，写入上传路径
        System.out.println(EssName);
        System.out.println(EssText);
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/file/") + File.separator;
            //上传文件名
            String filename = file.getOriginalFilename();
            File filepath = new File(path,filename);
            //判断路径是否存在，如果不存在就创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            file.transferTo(new File(path + File.separator +filename));
           return new responseData(200,"提交成功");
        } else {
            return new responseData(500,"提交失败");
        }
    }
}
